package com.zhiyi.xppay.ui;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhiyi.xppay.R;
import com.zhiyi.xppay.utils.AES;
import com.zhiyi.xppay.utils.AbSharedUtil;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author SuXiaoliang
 * @ClassName: SettingActivity
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @date 2018年6月23日 下午1:26:51
 */
public class SettingActivity extends Activity implements OnClickListener {

	private EditText et_data;
	private Button bt_save, bt_back;
	private RelativeLayout rl_back;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		setContentView(R.layout.activity_setting);

		et_data= (EditText) findViewById(R.id.et_data);

		if (!TextUtils.isEmpty(AbSharedUtil.getString(getApplicationContext(), "data"))) {
			et_data.setText(AbSharedUtil.getString(getApplicationContext(), "data"));
		}
		bt_save = (Button) findViewById(R.id.save);
		bt_back = (Button) findViewById(R.id.back);
		rl_back = (RelativeLayout) findViewById(R.id.rl_back);
		bt_back.setOnClickListener(this);
		bt_save.setOnClickListener(this);
		rl_back.setOnClickListener(this);

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.save:
				String data = et_data.getText().toString();
				if (TextUtils.isEmpty(data)) {
					Toast.makeText(getApplicationContext(), "配置数据不能为空！", Toast.LENGTH_LONG).show();
					return;
				}
				readingData(data);
				Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_LONG).show();
				break;
			case R.id.back:
				finish();
				break;
			case R.id.rl_back:
				finish();
				break;
			default:
				break;
		}
	}
	public void readingData(String data){
		JSONObject jsonObjs;
		String host,userId, applyId,key;
		try {
			String decodeData = AES.decode(data);

			jsonObjs = new JSONObject(decodeData);
			host = jsonObjs.getString("host");
			userId = jsonObjs.getString("userId");
			applyId =jsonObjs.getString("applyId");
			key = jsonObjs.getString("key");
		} catch (JSONException e) {
			e.printStackTrace();
			Toast.makeText(getApplicationContext(), "数据错误，请您输入网站上显示的配置数据!", Toast.LENGTH_SHORT).show();
			return;
		}
		if(TextUtils.isEmpty(userId) || TextUtils.isEmpty(applyId) || TextUtils.isEmpty(key)){
			Toast.makeText(getApplicationContext(), "数据错误，请您输入网站上显示的配置数据!", Toast.LENGTH_SHORT).show();
			return;
		}

		AbSharedUtil.putString(getApplicationContext(), "wsurl","ws://"+host+"/websocket");
		AbSharedUtil.putString(getApplicationContext(), "url","http://"+host+"/appPush");
		AbSharedUtil.putString(getApplicationContext(), "userId",userId);
		AbSharedUtil.putString(getApplicationContext(), "applyId",applyId);
		AbSharedUtil.putString(getApplicationContext(), "key", key);

		AbSharedUtil.putString(getApplicationContext(), "data", data);

	}
}
